<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\admin\controller\api;
use app\common\controller\Api;
/**
 * 角色
 * @package app\admin
 */
class Role extends Api

{

    /**
   * 角色列表
   * 
   * @return  json 
   * 
   *
   */

	public function roleList(){
             $model = model('common/Role');

             if(($limit = input('limit')) && ($page = input('page'))){
					$startLimit =  ($page-1)*$limit;
					//角色列表
					$list =collection(model('Role')->limit($startLimit.','.$limit)->select())->toArray();
		}else{
					//角色列表
					$list =collection(model('Role')->select())->toArray();
			}
 
            $count = $model->count('id');
            if ($list) {
                   return json(['code'=>1,'msg'=>'角色列表','count'=>$count,'data'=>$list]);
            }else{
                   return json(['code'=>0,'msg'=>'数据异常,请检查','count'=>$count]);
            }
    }
   	  
  /**
   * 添加角色
   * 
   * @return  json 
   * @param array data 表单数据,
   *
   */
   	 public function add($data){
       $model = model('common/Role');
         if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
        $result = $model->change($data);
        if (false != $result) {
          setlog('Roleadd','Role',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'角色添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'添加失败,请检查']);
          }
}


   	
   /**
   * 修改角色
   * @param string roleId 角色id,data修改的角色数据
   * @return json
   *
   */
   	public function edit($data=null,$roleId=null){
          
          if (empty($roleId)) {
              return json(['code'=>0,'msg'=>'角色不存在,请检查']);
          }

          $model = model('common/Role');

          if($roleId==1) {
            return json(['code'=>0,'msg'=>'超级管理员不允许更改']);
          }
          if ($model->change($data)) {
             setlog('Roleupdate','Role',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'角色修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'角色修改失败,请检查']);
          }
       }


    /**
   *  删除角色
   * @param  string roleid 角色id,
   * @return  json
   *
   */
     
    public function del($roleId) {

            if (empty($roleId)) {
               return  json(['code' => 0,'msg' => '角色id不存在,请检查']);
            }
            $model =  model('common/Role'); 
            $user    =  db('roleaccess');
            if ($roleId==1) {
                return  json(['code' => 0,'msg' => '超级管理员不能删除']);
            }
               $count=$user->where(array('roleId'=>$roleId))->count();
            if ($count > 0) {
               return  json(['code' => 0,'msg' => '该角色已经有用户,请检查']);
            }else{
              if ($model->destroy(['id' => $roleId]) !== FALSE) {
                setlog('Roledel','Role',session('user_auth.id'));
                 return  json(['code' => 1,'msg' => '角色删除成功']);
              }else{
                  return  json(['code' => 0,'msg' => '角色删除失败,请检查']);
              }
            } 
        }

     
   /**
   *   查询角色
   * @param string  keyword角色名称
   * @return  json
   *
   */

   	public function search($keyword=null){
        	if($keyword){
                        if(($limit = input('limit')) && ($page = input('page'))){
                        $startLimit =  ($page-1)*$limit;
                        $info =collection(model('common/Role')->where('roleName','like','%'.$keyword.'%')->limit($startLimit.','.$limit)->select())->toArray();
                         }else{
                        $info =collection(model('common/Role')->where('roleName','like','%'.$keyword.'%')->select())->toArray();
                      }
        	   }else{
                     if(($limit = input('limit')) && ($page = input('page'))){
                       $startLimit =  ($page-1)*$limit;
                       $info =collection(model('common/Role')->limit($startLimit.','.$limit)->select())->toArray();
                     }else{
                       $info =collection(model('common/Role')->select())->toArray();
                     }
                  }
          	
    	    //总数
	      $count = count($info); 
             if (empty($info) && $count == 0) {
         	    return json(['code'=>0,'msg'=>'没有找到角色']);
       	} 
       	
             if ($count > 0){
       		return json(['code'=>1,'msg'=>'角色信息','count'=>$count,'data'=>$info]);
       	 }
      }


  /**
   *  权限列表
   * @param  
   * @return  json
   *
   */
     
    public function getRuleList() {
        $rule = model('common/Rule');

        //权限列表
        $rules = $rule->where(array('status' => 1))->order(array( 'path' => 'asc'))->select();
        //分级
        $level1 = [];
        $level2 = [];
        $level3 = [];
        $data   = [];
       foreach($rules as $v){
            if($v['level'] == 0){
                $level1[] = $v;
            }
            if($v['level'] == 1){
                $level2[] = $v;
            }
            if($v['level'] == 2){
                $level3[] = $v;
            }
        }
        $data['level1']=$level1;
        $data['level2']=$level2;
        $data['level3']=$level3;

      return json(['code'=>1,'msg'=>'权限分级列表','data' => $data]);
        
      }
      
      /**
       * 园区片区列表
       * @return \think\response\Json
       */
      public function getAreaTree() {
      	$library = model('Library');
      	//工业园分区
      	$area = collection($library->where('sysCode',1)->select())->toArray();
      	$data = [];
      	foreach ($area as $item){
      		$subArea = collection($library->where('parentCode',1)->where('parentSubsystemNo',$item['subsystemNo'])->select())->toArray();
      		$item['child'] = $subArea;
      		$data[] = $item;
      	}
      	return json(['code'=>1,'msg'=>'片区列表','data'=>$data]);
      }


    

}